package gormUtils

import (
	"gorm.io/gen"
	"gorm.io/gen/field"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"utilsx/db/db"
	"utilsx/entity"
)

func NN(nu int) *gorm.DB {
	switch nu {
	case 0:
		return db.InitMysql(db.DataBase{
			Username: "root",
			Password: "root",
			Host:     "192.168.8.20:3307",
			DataBase: "1PanelTest",
		})
	case 1:
		return db.InitMysql(db.DataBase{
			Username: "root",
			Password: "root",
			Host:     "192.168.8.20:3307",
			DataBase: "1Panel",
		})
	case 2:
		db.InitMysql(db.DataBase{
			Username: "root",
			Password: "root",
			Host:     "67.21.86.154:3306",
			DataBase: "1Panel",
		})
	case 3:
		return db.InitMysql(db.DataBase{
			Username: "root",
			Password: "root",
			Host:     "192.168.8.20:3307",
			DataBase: "singer",
		})
	default:
		panic("ss")
	}

	return nil
}
func Gen(mysql *gorm.DB, models entity.Set[string, schema.Tabler],
	dataMap entity.Set[string, func(gorm.ColumnType) (dataType string)],
	modelPath, outPath string, opts ...gen.ModelOpt) {
	generator := gen.NewGenerator(gen.Config{
		ModelPkgPath:     modelPath,
		OutPath:          outPath,
		FieldWithTypeTag: true, // generate with gorm column type tag
		Mode:             gen.WithDefaultQuery | gen.WithQueryInterface,
	})
	generator.UseDB(mysql)
	//var dataMap = map[string]func(gorm.ColumnType) (dataType string){
	//	"tinyint":   func(detailType gorm.ColumnType) (dataType string) { return "int32" },
	//	"smallint":  func(detailType gorm.ColumnType) (dataType string) { return "int32" },
	//	"mediumint": func(detailType gorm.ColumnType) (dataType string) { return "int32" },
	//	"bigint":    func(detailType gorm.ColumnType) (dataType string) { return "int32" },
	//	"int":       func(detailType gorm.ColumnType) (dataType string) { return "int32" },
	//}
	generator.WithDataTypeMap(dataMap)
	for _, v := range models {
		//basic(generator, v)
		generator.ApplyInterface(func() {},
			generator.GenerateModel(v.TableName(), opts...))
	}
	generator.Execute()
}

func basic(generator *gen.Generator, v schema.Tabler) {
	generator.ApplyInterface(func() {},
		generator.GenerateModel(v.TableName(),
			gen.FieldType("mediaIds", "[]int32"),
			gen.FieldGORMTag("mediaIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("playlistIds", "[]int32"),
			gen.FieldGORMTag("playlistIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("labelIds", "[]int32"),
			gen.FieldGORMTag("labelIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("artistIds", "[]int32"),
			gen.FieldGORMTag("artistIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("artistNames", "[]string"),
			gen.FieldGORMTag("artistNames", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("alias", "[]string"),
			gen.FieldGORMTag("alias", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("artists", "[]BaseInfo"),
			gen.FieldGORMTag("artists", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("itemIds", "[]int32"),
			gen.FieldGORMTag("itemIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("topicIds", "[]int32"),
			gen.FieldGORMTag("topicIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("genreIds", "[]int32"),
			gen.FieldGORMTag("genreIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),

			gen.FieldType("categorizeIds", "[]int32"),
			gen.FieldGORMTag("categorizeIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("otherIds", "[]int32"),
			gen.FieldGORMTag("otherIds", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("playUrls", "[]string"),
			gen.FieldGORMTag("playUrls", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
			gen.FieldType("posterImgUrls", "[]string"),
			gen.FieldGORMTag("posterImgUrls", func(tag field.GormTag) field.GormTag {
				tag.Set("serializer", "json")
				return tag
			}),
		))
}

func ModelOpt(collection map[string]string) []gen.ModelOpt {
	var data []gen.ModelOpt
	for columnName, newType := range collection {
		data = append(data, gen.FieldType(columnName, newType))
		data = append(data, gen.FieldGORMTag(columnName, func(tag field.GormTag) field.GormTag {
			tag.Set("serializer", "json")
			return tag
		}))
	}
	return data

}
